package com.eims.sls_app.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.VolleyLog;
import com.eims.sls_app.dataaccess.AppGateway;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.FollowInfoActivity.MyAsynTask;
import com.eims.sls_app.ui.adapter.AppGatewayfollowAdapter;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.BaseHelper;

import bls.juejin.cn.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WithFollowdetailsActivity extends MyBaseActivity implements OnClickListener {

	private TextView tv_details_title;
	private ImageButton btn_back_with;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数
	private MyAsynTask myAsynTask;
	private MyHandler myHandler;
	// dialog
	private ProgressDialog dialog;
	// List集合
	public List<AppGateway> list_appGateWay;
	// listview
	public ListView listView;
	// adapter
	public AppGatewayfollowAdapter appGatewayAdapter;
	private MyAsynTaskBind asynTaskBind;
	public String id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_with_follow_details);
		
		initView();
		asynTaskBind = new MyAsynTaskBind();
		asynTaskBind.execute();
		
	}

	@SuppressWarnings("unchecked")
	private void initView() {
		
		Intent intent=getIntent();
		Bundle bundle=intent.getExtras();
		id=bundle.getString("id");
		Log.e("x", id+"");
		myHandler = new MyHandler();
		tv_details_title = (TextView) findViewById(R.id.tv_copy_list);
		btn_back_with = (ImageButton) findViewById(R.id.btn_back_with);
		listView=(ListView) findViewById(R.id.listview_follow);
		
		btn_back_with.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_back_with:
			finish();
			break;

		default:
			break;
		}
		
	}
	
	/** 处理页面显示的 */
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
		
		
			case 2000:
				if (list_appGateWay.size() > 0) {
					appGatewayAdapter = new AppGatewayfollowAdapter(
							WithFollowdetailsActivity.this, list_appGateWay);
					listView.setAdapter(appGatewayAdapter);
					tv_details_title.setText("跟单人数("+list_appGateWay.size()+")");
				}
				break;
			default:
				break;
		
			}
			super.handleMessage(msg);
		}

	}

	/**
	 * 异步任务
	 */
	class MyAsynTaskBind extends AsyncTask<Void, Integer, String> {
		String error = "-1001";

		@Override
		protected String doInBackground(Void... params) {
			opt = "81";
			info = RspBodyBaseBean.changeJC_info(id + "");

			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(getApplicationContext());
			String key = App.key;
			if (App.user == null) {
				crc = RspBodyBaseBean.getCrc(time, imei, key, info, "-1");
				auth = RspBodyBaseBean.getAuth(crc, time, imei, "-1");
			} else {
				crc = RspBodyBaseBean.getCrc(time, imei, key, info,
						App.user.getUid());
				auth = RspBodyBaseBean.getAuth(crc, time, imei,
						App.user.getUid());
			}

			String[] values = { opt, auth, info };
			String result1 = HttpUtils.doPost(App.names, values,
					App.AppGateway);
			Log.e("x", "绑定信息的result----36-ada---" + result1);
			Log.e("x", result1);
			try {
				JSONArray jsonObjs = new JSONArray(result1);
				list_appGateWay = new ArrayList<AppGateway>();
				for (int i = 0; i < jsonObjs.length(); i++) {
					AppGateway appGateway = new AppGateway();
					JSONObject jsonObject = (JSONObject) jsonObjs.get(i);
					appGateway.setName(jsonObject.getString("Name").substring(
							0, 2)
							+ "***");
					appGateway.setDetailMoney(jsonObject
							.getString("DetailMoney"));
					appGateway.setShare(jsonObject.getString("Share"));
					appGateway.setDateTime(jsonObject.getString("Datetime"));
					list_appGateWay.add(appGateway);
				}
				Message message = new Message();
				message.what = 2000;
				myHandler.sendMessage(message);
			} catch (Exception e) {
				VolleyLog.e("JSONObject Exception:%s , response:%s",
						e.toString(), result1);
			}
			return error;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = BaseHelper.showProgress(WithFollowdetailsActivity.this, null, "加载中....", true,
					true);
		}

		@Override
		protected void onPostExecute(String result) {
			if (dialog != null)
				dialog.dismiss();
			myHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}

	}


}
